package cn.xiaoyu.su.listener;

import cn.xiaoyu.su.enums.BizCodeEnum;
import cn.xiaoyu.su.exception.BizException;
import cn.xiaoyu.su.model.EventMessage;
import cn.xiaoyu.su.service.TrafficService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queuesToDeclare = {
        @Queue("order.traffic.queue"),
        @Queue("traffic.free_init.queue")
})
@Slf4j
public class TrafficMQListener {

    @Autowired
    private TrafficService trafficService;

    @RabbitHandler
    public void trafficHandler(EventMessage eventMessage, Message message, Channel channel) {

        log.info("监听到消息trafficHandler：{}",eventMessage);
        try{

            trafficService.handleTrafficMessage(eventMessage);

        }catch (Exception e){
            log.error("消费者失败：{}",eventMessage);
            throw new BizException(BizCodeEnum.MQ_COSUME_EXCEPTION);
        }

        log.info("消费者成功：{}",eventMessage);
    }
}
